﻿Public Class frmSearchLog

    Private dt As DataTable
    Private Sub searchLog_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dtp1.Value = Now
        dtp2.Value = Now
        st1.BackColor = Color.White
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

        If dtp1.Value > dtp2.Value Then
            MsgBox(getMsgStr("MSG10012", "开始时间", "结束时间"))
            dtp1.Focus()
            Exit Sub
        End If

        If txtEquipNo.Text = "" Then
            MsgBox(getMsgStr("MSG00002", "设备号"))
            txtEquipNo.Focus()
            Exit Sub
        End If

        txtEquipNo.Text = txtEquipNo.Text.ToUpper
        tssl.Text = ""
        tssl.BackColor = Color.White
        tssl.ForeColor = Color.Blue
        tssl2.Visible = False
        GetData()

        If Not IsNothing(dt) Then
            dgv.DataSource = dt
            dgv.AutoGenerateColumns = False
        End If

    End Sub

    Public Sub GetData()
        Dim szSql As String = ""
        'UPD 苗 2018/1/31
        szSql &= "SELECT * FROM TRA.L_ICTAG_OPE_LOG "
        szSql &= " WHERE EQUIP_ID=? and yyyymmdd>=? and yyyymmdd<=?  "
        szSql &= "  ORDER BY OPT_TIME DESC "

        Dim d1 As Integer = CInt(dtp1.Value.Year.ToString & dtp1.Value.Month.ToString("00") & dtp1.Value.Day.ToString("00"))
        Dim d2 As Integer = CInt(dtp2.Value.Year.ToString & dtp2.Value.Month.ToString("00") & dtp1.Value.Day.ToString("00"))
        tssl.Text = "查询中..."
        tssl.BackColor = Color.White
        tssl.ForeColor = Color.Blue
        Me.Refresh()
        Try
            dt = DBacs.ExecuteSql(szSql, -1, txtEquipNo.Text, d1, d2)
            tssl.Text = "共查出" & IIf(IsNothing(dt), 0, dt.Rows.Count) & "条数据"
            tssl.BackColor = Color.White
            tssl.ForeColor = Color.Blue
            tssl2.Visible = True
        Catch ex As Exception
            tssl.Text = "查询中出错，可尝试调整查询条件后再进行查询！"
            tssl.BackColor = Color.Red
            tssl.ForeColor = Color.White
        End Try

    End Sub

    Private Sub dgv_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgv.CellMouseDown
        If e.Button = MouseButtons.Right Then
            If e.RowIndex >= 0 Then

                cms.Show(MousePosition.X, MousePosition.Y)

            End If
        End If
    End Sub

    Private Sub btnExcel_Click(sender As Object, e As EventArgs) Handles btnExcel.Click
        outputExcel(dgv)
    End Sub

    Private Sub itemFilter_Click(sender As Object, e As EventArgs) Handles itemFilter.Click
        txtFilter.Text = dgv.Columns(dgv.SelectedCells(0).ColumnIndex).Name & " = '" & dgv.SelectedCells(0).Value & "'"
    End Sub

    Private Sub itemAddFilter_Click(sender As Object, e As EventArgs) Handles itemAddFilter.Click
        If txtFilter.Text = "" Then
            txtFilter.Text = dgv.Columns(dgv.SelectedCells(0).ColumnIndex).Name & " = '" & dgv.SelectedCells(0).Value & "'"
        Else
            txtFilter.Text &= " and " & dgv.Columns(dgv.SelectedCells(0).ColumnIndex).Name & " = '" & dgv.SelectedCells(0).Value & "'"
        End If
    End Sub

    Private Sub btnFilter_Click(sender As Object, e As EventArgs) Handles btnFilter.Click
        If IsNothing(dt) Then Exit Sub
        'If txtFilter.Text = "" Then Exit Sub
        Try
            dt.DefaultView.RowFilter = txtFilter.Text
        Catch ex As Exception
            tssl.Text = "过滤条件不正确，请修改！"
            tssl.ForeColor = Color.Red
            txtFilter.Focus()
        End Try
    End Sub

    Private Sub dgv_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellDoubleClick
        Dim seldr As DataGridViewRow = dgv.CurrentRow
        Dim frmDetail As New frmLogDetail
        frmDetail.Dr = seldr
        frmDetail.ShowDialog()

    End Sub

End Class